/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_SpaceTime.c
 *
 *  Created on: 2023年9月11日
 *      Author: 64435
 */

#include "LOS_SpaceTime.h"

uint8_t SpaceTime_TMDATA[42];
uint8_t ST_PollFlag = 0;

void LOS_SpaceTime_GetSta()
{
    LOS_SNP_t SNP_CMD;                  //首部配置结构体
    uint8_t FP_CMD[8];                  //首部内容
    SNP_CMD.SNP_dest = nID_Spacetime;     
    SNP_CMD.SNP_source = 0xF0;          //OBC发送
    SNP_CMD.SNP_rec = 0x00;             //内容识别码：0
    SNP_CMD.SNP_len = 8+8;              //长度（包含首部）
    SNP_CMD.SNP_tran = 0x01;            //0001B,信息传递服务
    LOS_CAN_FirstPart(&SNP_CMD,FP_CMD);
    FP_CMD[7] -= 1;
    LOS_CAN_Transmit(LOS_CAN_NUM1,0X18A6F000,FP_CMD,8);
    FP_CMD[0] = 0X01;
    FP_CMD[1] = 0X01;
    FP_CMD[2] = 0XFF;
    FP_CMD[3] = 0XFF;
    FP_CMD[4] = 0XFF;
    FP_CMD[5] = 0XFF;
    FP_CMD[6] = 0XFF;
    FP_CMD[7] = 0XFF;
    LOS_CAN_Transmit(LOS_CAN_NUM1,0X18A6F0FF,FP_CMD,8);
}

void LOS_SpaceTime_StartDT()
{
    LOS_SNP_t SNP_CMD;                  //首部配置结构体
    uint8_t FP_CMD[8];                  //首部内容
    SNP_CMD.SNP_dest = nID_Spacetime;     
    SNP_CMD.SNP_source = 0xF0;          //OBC发送
    SNP_CMD.SNP_rec = 0x00;             //内容识别码：0
    SNP_CMD.SNP_len = 8+8;              //长度（包含首部）
    SNP_CMD.SNP_tran = 0x01;            //0001B,信息传递服务
    LOS_CAN_FirstPart(&SNP_CMD,FP_CMD);
    FP_CMD[7] -= 1;
    LOS_CAN_Transmit(LOS_CAN_NUM1,0X18A6F000,FP_CMD,8);
    FP_CMD[0] = 0X02;
    FP_CMD[1] = 0X00;
    FP_CMD[2] = 0XFF;
    FP_CMD[3] = 0XFF;
    FP_CMD[4] = 0XFF;
    FP_CMD[5] = 0XFF;
    FP_CMD[6] = 0XFF;
    FP_CMD[7] = 0XFF;
    LOS_CAN_Transmit(LOS_CAN_NUM1,0X18A6F0FF,FP_CMD,8);
}

void LOS_CAN_STCallBack(LOS_CANMSG_t* can_re)
{
    //首部校验用法：首部校验通过/未通过会改变canerrflag的值，所有消息会被先存在缓冲区，canerrflag有效时才会转存，无效时舍弃
    uint8_t id_filter[4];
    uint8_t sum = 0;                       //首部校验
    uint8_t sum_check = 0;                 //首部校验
    static uint8_t canerrflag = 0;         //表示首部校验是否通过，未通过：1，通过：0

    memcpy(id_filter, &(can_re->ID), 4);   //将帧ID分为4个字节存储，便于处理
    if (id_filter[3]==0x00)                //若为首帧
    {
        sum = can_re->msg[0] + can_re->msg[1] + can_re->msg[2] + can_re->msg[3] + can_re->msg[4] + can_re->msg[5] + can_re->msg[6];
        sum_check = 0xFF - sum;
        if (sum_check == can_re->msg[7])   //校验和检查，消息有效性判断
        {
            canerrflag = 0;
        }
        else 
        {
           canerrflag = 1;
        }

    }
    else if (id_filter[3]==0xFF)  //若为尾帧
    {
        if (canerrflag)
        {
            LOS_CAN_DoNothing();
        }
        else 
        {
            memcpy(&SpaceTime_TMDATA[34],can_re->msg,8);
        }
        canerrflag = 0;   //最后一帧时清除标志位
        
    }
    else   //若为中间帧
    {
        if (canerrflag)
        {
            LOS_CAN_DoNothing();
        }
        else 
        {
            if (id_filter[3] == 0x01)
            {
                memcpy(&SpaceTime_TMDATA[0], &LOS_Time_SecFromBoot, 4);
                memcpy(&SpaceTime_TMDATA[4], &LOS_Time_MilSec, 2);
                memcpy(&SpaceTime_TMDATA[6], &LOS_Time_Sec, 4);
                memcpy(&SpaceTime_TMDATA[10],can_re->msg,8);
            }
            else if (id_filter[3] == 0x02)
            {
                memcpy(&SpaceTime_TMDATA[18],can_re->msg,8);
            }
            else if (id_filter[3] == 0x03)
            {
                memcpy(&SpaceTime_TMDATA[26],can_re->msg,8);
            }
        }
    }
}

void LOS_SpaceTime_PWROn()
{
    LOS_EPS_Switch(5, SWITCH_ON);
}

void LOS_SpaceTime_PWROff()
{
    LOS_EPS_Switch(5, SWITCH_OFF);
}
